<?php
/**
 * 评论类
 * @gaojian3
 * 
 */
class ml_model_dbComment extends Lib_datamodel_db
{
    public function __construct() {
        
        $db_config = ml_factory::load_standard_conf ( 'dbContent' );
        parent::__construct ( 'comment', $db_config ['comment'] );
    }
    
    /**
     * 发表评论
     * @gaojian3
     * @param array $data
     * @return unknown 
     */
    public function insertComment($data)
    {
        $uid = $data['uid'];
        if(!$this->init_db($uid , self::DB_MASTER ))
        return false;
        
        $arrInsert = array(
            'rid' => $data['rid'],
            'uid' => $data['uid'],
            'act_uid' => $data['act_uid'],
            'reply_id'=>$data['reply_id'],
            'content_rid'=>$data['content_rid'],
            'content' => $data['content'],
            'from' => empty($data['from']) ? 1 : $data['from'],
            'cip' => Tool_ip::get_real_ip()
        );
        
        return $this->insert($arrInsert);
    }

    /**
     * 通过rid获取评论(单条或多条)
     * @gaojian3
     * @param sting $rid
     * @param sting $begin 0
     * @param sting $num  20
     * @return unknown 
     */
    public function getCommentsByRid($rid,$begin='0',$num='0',$from= ML_FROM_WEB )
    {
        $uid = ml_tool_resid::resid2uid($rid);
        if(!$this->init_db($uid , self::DB_SLAVE ))
        return false;
        $where='';
        if( $from != ML_FROM_WEB ){
            $where ="  AND  `from` != '$from' ";
        }
        
        $fields = ' `com_id`,`rid`,`act_uid`,`content_rid`,`content`,`from`,`ctime`,`reply_id`  ';
        $limit = '';
        
        if($num != '0'){
            $limit = ' LIMIT '.intval($begin).','.intval($num);
        }
        
        $sql = 'SELECT '.$fields.' FROM `'.$this->table."`  WHERE `rid` ='{$this->escape($rid)}' ". $where ." AND  `status` != 9   ORDER BY `ctime` DESC  ".$limit;
        if($num-$begin == '1') 
            return $this->fetch_row($sql);
        else
            return $this->fetch($sql);
    }
    
    public function getLatestCommentByRid($rid)
    {
        $uid = ml_tool_resid::resid2uid($rid);
        if(!$this->init_db($uid , self::DB_SLAVE ))
        return false;
        
        $fields = ' `com_id`,`rid`,`act_uid`,`content_rid`,`content`,`from`,`ctime`,`reply_id`  ';
                
        $sql = 'SELECT '.$fields.' FROM `'.$this->table."`  WHERE `rid` ='{$this->escape($rid)}'  AND  `status` != 9   AND  `reply_id` = 0  ORDER BY `ctime` DESC  ";
        return $this->fetch_row($sql);
        
    }
    /**
     * 为方便我收到评论页面显示，特修改uid
     * 
     * @param unknown_type $uid
     * @param unknown_type $com_id
     * @return unknown
     */
    
    public function updateUid($uid,$com_id){
        if(!$this->init_db($uid, self::DB_MASTER ))
        return false;
        $where='`com_id` ='.$com_id;
        return $this->update(array('uid'=>'0'),$where);
    }
    
    /**
     * 获取评论数
     * @gaojian3
     * @param string $rid
     * @return unknown  
     */
    public function getCountsByRid($rid)
    {
        $uid = ml_tool_resid::resid2uid($rid);
        if(!$this->init_db($uid , self::DB_SLAVE  ))
        return false;
        
        $sql='select count(*) as num from `'.$this->table."`  where `rid`= '{$this->escape($rid)}' AND  `status` != 9   ";
        return $this->fetch_row($sql);
    }
    
    /**
     * 获取 非手机 评论数  （手机版使用）
     * @gaojian3
     * @param string $rid
     * @return unknown  
     */
    public function getCountNotWapByRid($rid)
    {
        $uid = ml_tool_resid::resid2uid($rid);
        if(!$this->init_db($uid , self::DB_SLAVE  ))
        return false;
        
        $sql='select count(*) as num from `'.$this->table."`  where `rid`= '{$this->escape($rid)}' AND `from`!= 2  AND  `status` != 9  ";
        return $this->fetch_row($sql);
    }
    
    /**
     * 删除一条评论
     * @gaojian3
     * @param int $com_id
     * @param int $uid
     * @param int $act_uid
     * @return unknown 
     */
    public function delCommentByCom_id($com_id,$uid,$act_uid)
    {
        if(!$this->init_db($uid, self::DB_MASTER  ))
        return false;
        
        $where='`com_id` ='.$com_id.' AND `act_uid` = '.$act_uid ;
        return $this->update(array('status'=>'9'),$where);
    }
    
    /**
     * 获取我收到的评论
     * @gaojian3
     * @param int $uid
     * @param sting $begin 0
     * @param sting $num  20
     * @return unknown 
     */
    public function getCommentsByUid($uid,$begin='0',$num='20')
    {
        if(!$this->init_db($uid, self::DB_SLAVE  ))
        return false;
        
        $fields = ' `com_id`,`uid`,`rid`,`act_uid`,`content_rid`,`content`,`from`,`ctime`,`reply_id`  ';
        $limit = '  ORDER BY `ctime` DESC LIMIT '.$begin.','.$num;
        $sql = 'SELECT '.$fields.' FROM `'.$this->table.'` WHERE   `status` != 9  AND `uid` = '.$uid.$limit;
        
        return $this->fetch($sql);
    }
    
    public function getComCountByUid($uid)
    {
        if(!$this->init_db($uid, self::DB_SLAVE  ))
        return false;

        $sql = '`status` != 9 AND  `uid` = '.$uid;
        
        return $this->fetch_count($sql);
    }
    
    
    /**
     * 根据条件获取评论
     *
     * @param unknown_type $condition
     * @param unknown_type $uid
     * @return unknown
     */
    public function getComByCondition($condition,$uid){
        if(!$this->init_db($uid, self::DB_SLAVE  ))
            return false;

        foreach($condition  as $key => $val  ){
            $sqlArr[]= "   `{$key}`  =  '{$this->escape($val)}' " ;    
        }
        $sqlStr=implode('  AND  ',$sqlArr);
        
        $sql = 'select * from '.$this->table.' where  `status` != 9  AND ' . $sqlStr ;
        return $this->fetch($sql);
        
    }
    
    
    /**
     * 根据comid获取评论内容
     * 
     * @param unknown_type $uid
     * @param unknown_type $com_id
     * @return unknown
     */
    public function getCommentById($uid , $com_id)
    {
        if(!$this->init_db($uid, self::DB_SLAVE))
        return false;
        
        $sql = 'select * from '.$this->table.' where com_id='.$com_id.' and uid = '.$uid;
        return $this->fetch_row($sql);
    }
    
    /**
     * 根据多个com_id获取多条评论
     * 
     * @param unknown_type $comids
     * @param unknown_type $uid
     * @return unknown
     */
    public function getCommentByComids($comids,$uid){
        if(!$this->init_db($uid, self::DB_SLAVE))
            return false;
        $sql = 'select * from '.$this->table.' where  `com_id` in ('.$comids.')    ';
        return $this->fetch($sql);
    }
    
}
?>